package com.ma.service.impl;

import com.ma.common.constant.MessageConstant;
import com.ma.common.exception.AccountLockedException;
import com.ma.common.exception.AccountNotFoundException;
import com.ma.common.exception.PasswordErrorException;
import com.ma.common.constant.MessageConstant;
import com.ma.common.exception.AccountLockedException;
import com.ma.common.exception.AccountNotFoundException;
import com.ma.common.exception.LoginFailedException;
import com.ma.common.exception.PasswordErrorException;
import com.ma.entity.Doctor;
import com.ma.mapper.DoctorMapper;
import com.ma.service.IDoctorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ldc
 * @since 2023-08-28
 */
@Service
public class DoctorServiceImpl  implements IDoctorService {

    @Resource
    private DoctorMapper doctorMapper;

    @Override
    public List<Doctor> findAll() {
        return doctorMapper.findAll();
    }

    @Override
    public Doctor login(String username, String password) {
        Doctor doctor = doctorMapper.getByUserName(username);
        if (doctor == null){
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        if (!password.equals(doctor.getPassword())){
            throw  new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (doctor.getStatus() == 0){
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        return doctor;
    }

    @Override
    public List<Doctor> selectDoctorByPage(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        return doctorMapper.selectDoctorByPage(offset,pageSize);
    }

    @Override
    public int delDoctorById(int id) {
        return doctorMapper.delDoctorById(id);
    }


}
